﻿using DevExpress.XtraGrid.Views.BandedGrid;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;

namespace NursingFormSystem.BilirubinMonitoring
{
    public partial class HistoryBilirubinMonitoring : Form
    {
        public NursingForm formService = new NursingForm();
        Tb_User LoginUser;//当前的登录信息
        PatientInfo PatientInfo;
        public HistoryBilirubinMonitoring(Tb_User loginUser, PatientInfo patientInfo)
        {
            InitializeComponent();
            this.LoginUser = loginUser;
            this.PatientInfo = patientInfo;
            BindControl();
            BindInfo();
        }
        #region GridView设置
        //GridView设置
        private void BindControl()
        {
            StatDate.DateTime = DateTime.Now.AddDays(-30);
            EndDate.DateTime = DateTime.Now;
            foreach (GridBand gridBand in this.bandedGridView1.Bands)//设置居中
            {
                foreach (GridBand gridBandSecond in gridBand.Children)
                {
                    gridBandSecond.OptionsBand.FixedWidth = true;
                    gridBandSecond.AppearanceHeader.Options.UseTextOptions = true;
                    gridBandSecond.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                    gridBandSecond.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
                }
                gridBand.OptionsBand.FixedWidth = true;
                gridBand.AppearanceHeader.Options.UseTextOptions = true;
                gridBand.AppearanceHeader.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
                gridBand.AppearanceHeader.TextOptions.VAlignment = DevExpress.Utils.VertAlignment.Center;
            }
        }
        #endregion

        #region 查询
        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindInfo();
        }
        #endregion

        #region 新增
        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddBilirubinMonitoring addForm = new AddBilirubinMonitoring(LoginUser, PatientInfo, string.Empty);
            addForm.ShowDialog();
            if (addForm.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion

        #region 修改
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount > 0)
            {
                string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                AddBilirubinMonitoring addForm = new AddBilirubinMonitoring(LoginUser, PatientInfo, Id);
                addForm.ShowDialog();
                if (addForm.DialogResult == DialogResult.OK)
                {
                    BindInfo();
                }
            }
        }
        #endregion

        #region 打印
        private void btnPrint_Click(object sender, EventArgs e)
        {
            BilirubinPrints f = new BilirubinPrints(PatientInfo, StatDate.DateTime, EndDate.DateTime);
            f.Show();
        }
        #endregion

        #region 删除
        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            if (MessageBox.Show("确定要删除数据吗？", "删除前确认", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                string id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
                var ret2 = formService.DeleteNursingFormRecord(id);
                MessageBox.Show(ret2.M);
                if (ret2.Code == 1)
                {
                    string dateStart = this.StatDate.Text;
                    string dateEnd = this.EndDate.Text;
                    BindInfo();
                }
            }
        }
        #endregion

        #region LoadData显示
        private void BindInfo()
        {
            if (this.PatientInfo == null) return;
            string patientid = this.PatientInfo.PATIENT_ID;//住院号
            int VisitId = PatientInfo.VISIT_ID;//住院次数
            int FormId = (int)FormType.Bilirubin;//18004  //表单ID

            var ret = formService.GetNursingFormRecordByDate(patientid, VisitId, FormId, this.StatDate.DateTime, this.EndDate.DateTime);
            DataTable dt = new DataTable();
            dt.Columns.Add("JianCeRiQi", typeof(string));
            dt.Columns.Add("ShiJian", typeof(string));
            dt.Columns.Add("DanHongSu", typeof(string));
            dt.Columns.Add("Id", typeof(string));
            if (ret.Code == 1)
            {
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderByDescending(p => p.AssessmentDate).ToList();
                foreach (NursingFormRecord record in list)
                {
                    var NursingFormData = record.NursingFormData;
                    Bilirubin fd = new Bilirubin();
                    fd = XmlUtil.XMLToObject<Bilirubin>(NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["JianCeRiQi"] = Convert.ToDateTime(record.AssessmentDate).ToString("yyyy-MM-dd");
                    newRow["ShiJian"] = DateTimeHelper.HHmmssToHHmm(fd.ShiJian);
                    newRow["DanHongSu"] = fd.DanHongSu;
                    //ID隐藏，双击触发事件
                    newRow["Id"] = record.Id;
                    dt.Rows.Add(newRow);
                }
            }
            gridControl1.DataSource = dt;
        }
        #endregion

        #region 双击事件
        private void bandedGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (bandedGridView1.RowCount <= 0) return;
            string Id = bandedGridView1.GetRowCellValue(bandedGridView1.FocusedRowHandle, "Id").ToString();
            AddBilirubinMonitoring add = new AddBilirubinMonitoring(LoginUser, PatientInfo, Id);
            add.ShowDialog();
            if (add.DialogResult == DialogResult.OK)
            {
                BindInfo();
            }
        }
        #endregion
    }
}